#!/usr/bin/make -f
# Debian rules file, originally written by Joey Hess and Craig Small.
# Modified for scim-waitzar by Seth N. Hetu


# Set the host type, for cross-compilation, or just to save time later.
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
	build_flags = --build=$(DEB_BUILD_GNU_TYPE)
else
	build_flags = --build=$(DEB_BUILD_GNU_TYPE) --host=$(DEB_HOST_GNU_TYPE)
endif


CFLAGS = -Wall -g

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	MAKEFLAGS += -j$(NUMJOBS)
endif


config.status: configure
	dh_testdir
	./configure $(build_flags) --prefix=/usr --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info --enable-shared

build: build-stamp
build-stamp:  config.status
	dh_testdir

	#Compile
	$(MAKE)

	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f build-indep-stamp build-arch-stamp

	#Clean the package (and keep useful warnings)
	[ ! -f Makefile ] || $(MAKE) distclean
ifneq "$(wildcard /usr/share/misc/config.sub)" ""
	cp -f /usr/share/misc/config.sub config.sub
endif
ifneq "$(wildcard /usr/share/misc/config.guess)" ""
	cp -f /usr/share/misc/config.guess config.guess
endif

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	# Install int debian/tmp
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp


# Build architecture-independent files (keep for later)
binary-indep: build install

# Build architecture-dependent files 
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs ChangeLog
	dh_installdocs
	dh_install --sourcedir=debian/tmp
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
#	dh_makeshlibs    #Commenting out for now; scim uses its own means of identifying shared libraries
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install

